﻿package src.hardware 
{
	import flash.display.Sprite;
	import src.hardware.rgbledmatrixdisplay.LED;
	import src.hardware.rgbledmatrixdisplay.RGBLEDMatrixDisplay;
	import src.interfaces.ITopLEDDisplay;
	
	/**
	* ...
	* @author Louis Tovar
	*/
	public class TopLEDDisplay extends Sprite implements ITopLEDDisplay
	{
		public static const TOP_LED_COLOR:uint = 0xF0CC12;
		private const LED_SPACING_RATIO:Number = 1.8;
		
		private var _displayWidth:Number;
		private var _ledColumns:Number
		
		
		private var topLedContainer:Sprite;
		private var topLedArray:Array;
		
		private var ledSpacing:Number;
		private var ledRadius:Number;
		
		
		public function TopLEDDisplay( p_columns:uint, p_width:Number = 100) 
		{
			_displayWidth = p_width
			_ledColumns = p_columns;
			
			initSelf();
		}
		
		private function initSelf():void
		{
			
			topLedContainer = new Sprite();
			topLedArray = new Array();
			
			addChild(topLedContainer);
			
			var blockWidth:int = _displayWidth / _ledColumns;
			ledSpacing = blockWidth / LED_SPACING_RATIO;
			ledRadius = (blockWidth - ledSpacing)/2;
			
			for (var i:uint = 0; i < _ledColumns; i++)
			{
				var newLED:LED = new LED(ledRadius);
				
				newLED.x = i * (ledRadius * 2 + ledSpacing) + ledSpacing;
				
				topLedContainer.addChild(newLED);
				topLedArray.push(newLED);
			}
			
			topLedContainer.x -= ledSpacing / 2;
			
			SetAuxLEDs(0);
		}
		
		public function SetAuxLEDs(InputLEDs:uint):void
		{
			var lightBits:String;
			
			InputLEDs = (InputLEDs < 255) ? InputLEDs : 255;
			
			lightBits = InputLEDs.toString(2);
			
			while (lightBits.length != 8)
			{
				lightBits = '0' + lightBits;
			}
			
			for (var topLEDIndex:uint = 0; topLEDIndex < lightBits.length; topLEDIndex++)
			{
				if ( int(lightBits.charAt(topLEDIndex)) )
				{
					(topLedArray[topLEDIndex] as LED).setLEDColor(TOP_LED_COLOR);
				}
				else
				{
					(topLedArray[topLEDIndex] as LED).setLEDColor(0x303030);
				}
			}
			
		}
		
		public function get displayWidth():Number { return _displayWidth; }
		
		public function get ledColumns():Number { return _ledColumns; }
	}
	
}